package logic

import (
	"context"
	"novel-mgr1/model"
	"strings"

	"novel-mgr1/userrpc/internal/svc"
	"novel-mgr1/userrpc/userrpc"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 发表评论
func (l *CommentAddLogic) CommentAdd(in *userrpc.CommentAddRequest) (*userrpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line
	var findUser model.User
	err := findUser.FindUserById(model.DB, in.Uid)
	if err != nil {
		return nil, err
	}
	if findUser.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	var findNovel model.Novel
	err = findNovel.FindNovelById(model.DB, in.Nid)
	if err != nil {
		return nil, err
	}
	if findNovel.ID == 0 {
		return nil, errors.New("作品不存在")
	}
	if strings.Contains("抄袭", in.Comment) || strings.Contains("涉政", in.Comment) {
		return nil, errors.New("有违规评论")
	}

	comment := model.Comment{
		Uid:      in.Uid,
		Nid:      in.Nid,
		Comments: in.Comment,
	}

	if err = comment.CommentAdd(model.DB); err != nil {
		return nil, errors.New("评论失败")
	}
	return &userrpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
